﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SP_XHWeb.Layouts.SP_XHWeb
{
    public partial class EmployeeDetails : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string webUrl = "/companyinfo/default.aspx";
            string listTitle = "EmployeeInfo";
            string itemID = Request["item_id"].ToString();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(itemID) && !string.IsNullOrEmpty(hostUrl))
                {
                    using (SPSite site = new SPSite(hostUrl+webUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists.TryGetList(listTitle);
                            if (list != null && list.ItemCount != 0)
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (int.Parse(item.ID.ToString()) == int.Parse(itemID))
                                    {
                                        lblImg.InnerHtml = item["照片"] != null && !string.IsNullOrEmpty(item["照片"].ToString()) ? UpdatePictureFormat(item["照片"].ToString()) : "/";
                                        lblName.InnerHtml = item["姓名"].ToString();
                                        lblSex.InnerHtml = item["性别"].ToString();
                                        lblBirthday.InnerHtml = item["生日"] != null ? DateTime.Parse(item["生日"].ToString()).ToString("MM-dd") : "";
                                        lblComp.InnerHtml = item["公司"].ToString();
                                        lblDept.InnerHtml = item["部门"].ToString();
                                        lblPosition.InnerHtml = item["职位"].ToString();
                                        lblTele.InnerHtml = item["固定电话"] != null && !string.IsNullOrEmpty(item["固定电话"].ToString()) ? item["固定电话"].ToString() : "/";
                                        lblMobile.InnerHtml = item["移动电话"] != null && !string.IsNullOrEmpty(item["移动电话"].ToString()) ? item["移动电话"].ToString() : "/";
                                        lblQQ.InnerHtml = item["QQ"] != null && !string.IsNullOrEmpty(item["QQ"].ToString()) ? item["QQ"].ToString() : "/";
                                        lblMSN.InnerHtml = item["MSN"] != null && !string.IsNullOrEmpty(item["MSN"].ToString()) ? item["MSN"].ToString() : "/";
                                        lblWeibo.InnerHtml = item["新浪微博"] != null && !string.IsNullOrEmpty(item["新浪微博"].ToString()) ? item["新浪微博"].ToString() : "/";
                                        lblBlog.InnerHtml = item["个人博客地址"] != null && !string.IsNullOrEmpty(item["个人博客地址"].ToString()) ? item["个人博客地址"].ToString() : "/";
                                        lblEmail.InnerHtml = item["电子邮件"] != null && !string.IsNullOrEmpty(item["电子邮件"].ToString()) ? item["电子邮件"].ToString() : "/";
                                        lblIntroduction.InnerHtml = item["自我介绍"] != null && !string.IsNullOrEmpty(item["自我介绍"].ToString()) ? item["自我介绍"].ToString() : "/";
                                        lblHobby.InnerHtml = item["兴趣爱好"] != null && !string.IsNullOrEmpty(item["兴趣爱好"].ToString()) ? item["兴趣爱好"].ToString() : "/";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private string UpdatePictureFormat(string pic)
        {
            string picstr = string.Empty;
            if (!string.IsNullOrEmpty(pic))
            {
                if (pic.Contains("<img"))
                {
                    string tempic = pic.Substring(pic.IndexOf("<img"));
                    picstr = tempic.Substring(0, tempic.IndexOf("/>")+2).Replace("margin:5px", "").Replace("margin: 5px", "");
                }
                else
                {
                    picstr = "/";
                }
            }
            return picstr;
        }
    }
}
